package cn.wolfcode.luowowo.QueryObject;

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

import java.io.Serializable;

@Getter
@Setter
public class QueryObject implements Serializable {
    private int currentPage = 1;
    private int pageSize = 5;
    private String keyword;

    private Pageable pageable;

    public Pageable getPageable() {
        if (pageable == null) {
            //如果沒有指定分页对象值 默认id倒序
            return PageRequest.of(currentPage - 1, pageSize, Sort.Direction.ASC, "_id");
        }
        return pageable;
    }

    public String geyKeyword() {
        return StringUtils.hasLength(keyword) ? keyword : null;
    }
}
